package com.hdwe.tools.pm.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.hdwe.tools.pm.domain.bo.PmTaskBo;
import com.hdwe.tools.pm.domain.vo.PmTaskVo;
import com.hdwe.tools.pm.service.IPmTaskService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 任务
 *
 * @author Lion Li
 * @date 2024-12-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pm/task")
public class PmTaskController extends BaseController {

    private final IPmTaskService pmTaskService;

    /**
     * 查询任务列表
     */
    @SaCheckPermission("pm:task:list")
    @GetMapping("/list")
    public TableDataInfo<PmTaskVo> list(PmTaskBo bo, PageQuery pageQuery) {
        return pmTaskService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询我的任务列表
     */
    @SaCheckPermission("pm:task:list")
    @GetMapping("/my")
    public TableDataInfo<PmTaskVo> myList(PageQuery pageQuery) {
        Long userId = LoginHelper.getLoginUser().getUserId();
        return pmTaskService.queryMyTasks(userId, pageQuery);
    }

    /**
     * 查询我的相关任务列表
     */
    @SaCheckPermission("pm:task:list")
    @GetMapping("/my/related")
    public TableDataInfo<PmTaskVo> myRelatedList(PageQuery pageQuery) {
        Long userId = LoginHelper.getLoginUser().getUserId();
        return pmTaskService.queryMyRelatedTasks(userId, pageQuery);
    }

    /**
     * 查询我的日报任务列表
     */
    @SaCheckPermission("pm:task:list")
    @GetMapping("/my/daily")
    public R<List<PmTaskVo>> dailyList() {
        Long userId = LoginHelper.getLoginUser().getUserId();
        return R.ok(pmTaskService.queryDailyReport(userId));
    }

    /**
     * 查询我的周报任务列表
     */
    @SaCheckPermission("pm:task:list")
    @GetMapping("/my/weekly")
    public R<List<PmTaskVo>> weeklyList() {
        Long userId = LoginHelper.getLoginUser().getUserId();
        return R.ok(pmTaskService.queryWeeklyTasks(userId));
    }

    /**
     * 查询我的里程碑任务列表
     */
    @SaCheckPermission("pm:task:list")
    @GetMapping("/milestone/{milestoneId}")
    public R<List<PmTaskVo>> milestoneList(@NotNull(message = "里程碑主键不能为空")
                                           @PathVariable Long milestoneId) {
        return R.ok(pmTaskService.queryMilestoneTasks(milestoneId));
    }

    /**
     * 查询我的需求任务列表
     */
    @SaCheckPermission("pm:task:list")
    @GetMapping("/require/{requireId}")
    public R<List<PmTaskVo>> requireList(@NotNull(message = "需求主键不能为空")
                                         @PathVariable Long requireId) {
        return R.ok(pmTaskService.queryRequireTasks(requireId));
    }

    /**
     * 导出任务列表
     */
    @SaCheckPermission("pm:task:export")
    @Log(title = "任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PmTaskBo bo, HttpServletResponse response) {
        List<PmTaskVo> list = pmTaskService.queryList(bo);
        ExcelUtil.exportExcel(list, "任务", PmTaskVo.class, response);
    }

    /**
     * 获取任务详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("pm:task:query")
    @GetMapping("/{id}")
    public R<PmTaskVo> getInfo(@NotNull(message = "主键不能为空")
                               @PathVariable Long id) {
        return R.ok(pmTaskService.queryById(id));
    }

    /**
     * 新增任务
     */
    @SaCheckPermission("pm:task:add")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PmTaskBo bo) {
        return toAjax(pmTaskService.insertByBo(bo));
    }

    /**
     * 修改任务
     */
    @SaCheckPermission("pm:task:edit")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PmTaskBo bo) {
        return toAjax(pmTaskService.updateByBo(bo));
    }

    /**
     * 删除任务
     *
     * @param ids 主键串
     */
    @SaCheckPermission("pm:task:remove")
    @Log(title = "任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(pmTaskService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 批量新增任务
     */
    @SaCheckPermission("pm:task:add")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/batchAdd")
    public R<Void> batchAdd(@Validated(AddGroup.class) @RequestBody List<PmTaskBo> boList) {
        return toAjax(pmTaskService.batchInsertByBo(boList));
    }
}
